package org.sport.logic;

import org.sport.dbImplement.ActOpeImp;
import org.sport.dbImplement.UserOpeImp;
import org.sport.dbInterface.AwardOperation;
import org.sport.init.SportServerSymbol;
import org.sport.model.entityModel.ActivitytableEntity;
import org.sport.model.entityModel.AwardtableEntity;
import org.sport.model.entityModel.DateInfo;
import org.sport.model.entityModel.UsertableEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by zehong
 *
 * 各种工具，比如根据用户电话号码查出用户整个信息
 */
@Service
public class LogicUtils {

    @Autowired
    UserOpeImp userOperation;

    @Autowired
    ActOpeImp actOperation;

    @Autowired
    AwardOperation awardOperation;


    //根据用户电话号码查找这个用户
    public UsertableEntity findUser(String phoneNumber)
    {
        UsertableEntity usertosearch = new UsertableEntity();
        usertosearch.setPhoneNumber(phoneNumber);
        UsertableEntity usertosearch2 = userOperation.findUserByPhone(usertosearch);

        return usertosearch2;
    }

    //根据活动ID查找某个活动
    public ActivitytableEntity findActByActid(int activity_id)
    {
        ActivitytableEntity activity=new ActivitytableEntity();
        activity.setId(activity_id);
        ActivitytableEntity activity2=actOperation.findByActId(activity);

        return activity2;
    }

    //根据用户找到用户签到表
    public AwardtableEntity findAwardByUser(UsertableEntity user){
        AwardtableEntity awardtosearch=new AwardtableEntity();
        awardtosearch.setUsertableByUserId(user);
        AwardtableEntity awardResult=awardOperation.findByUserId(awardtosearch);

        return awardResult;
    }


    //返回今天等之前一周的日期信息
    public DateInfo getWeekDateInfo(){

        DateInfo dateInfo=new DateInfo();

        Timestamp begin;
        Timestamp end;

        Calendar c=Calendar.getInstance();
        Calendar cBegin=Calendar.getInstance();
        Calendar cEnd=Calendar.getInstance();

        cBegin.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), 0, 0, 0);
        cEnd.set( c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), 23, 59, 59);

        SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //得到今天的起始以及结束时间
        begin=Timestamp.valueOf(format.format(cBegin.getTime()));
        end=Timestamp.valueOf(format.format(cEnd.getTime()));
        System.out.println(begin+"            "+end);
        //加入返回数据的今天部分
        dateInfo.setToday_begin(begin);
        dateInfo.setToday_end(end);


        //得到1天前的起始以及结束时间
        cBegin.add(Calendar.DATE, -1);
        cEnd.add(Calendar.DATE, -1);
        begin=Timestamp.valueOf(format.format(cBegin.getTime()));
        end=Timestamp.valueOf(format.format(cEnd.getTime()));
        System.out.println(begin+"            "+end);
        //加入返回数据的1天前部分
        dateInfo.setTodayBefore1_begin(begin);
        dateInfo.setTodayBefore1_end(end);

        //得到2天前的起始以及结束时间
        cBegin.add(Calendar.DATE, -1);
        cEnd.add(Calendar.DATE, -1);
        begin=Timestamp.valueOf(format.format(cBegin.getTime()));
        end=Timestamp.valueOf(format.format(cEnd.getTime()));
        System.out.println(begin+"            "+end);
        //加入返回数据的2天前部分
        dateInfo.setTodayBefore2_begin(begin);
        dateInfo.setTodayBefore2_end(end);


        //得到3天前的起始以及结束时间
        cBegin.add(Calendar.DATE, -1);
        cEnd.add(Calendar.DATE, -1);
        begin=Timestamp.valueOf(format.format(cBegin.getTime()));
        end=Timestamp.valueOf(format.format(cEnd.getTime()));
        System.out.println(begin+"            "+end);
        //加入返回数据的1天前部分
        dateInfo.setTodayBefore3_begin(begin);
        dateInfo.setTodayBefore3_end(end);


        //得到4天前的起始以及结束时间
        cBegin.add(Calendar.DATE, -1);
        cEnd.add(Calendar.DATE, -1);
        begin=Timestamp.valueOf(format.format(cBegin.getTime()));
        end=Timestamp.valueOf(format.format(cEnd.getTime()));
        System.out.println(begin+"            "+end);
        //加入返回数据的1天前部分
        dateInfo.setTodayBefore4_begin(begin);
        dateInfo.setTodayBefore4_end(end);


        //得到5天前的起始以及结束时间
        cBegin.add(Calendar.DATE, -1);
        cEnd.add(Calendar.DATE, -1);
        begin=Timestamp.valueOf(format.format(cBegin.getTime()));
        end=Timestamp.valueOf(format.format(cEnd.getTime()));
        System.out.println(begin+"            "+end);
        //加入返回数据的1天前部分
        dateInfo.setTodayBefore5_begin(begin);
        dateInfo.setTodayBefore5_end(end);


        //得到6天前的起始以及结束时间
        cBegin.add(Calendar.DATE, -1);
        cEnd.add(Calendar.DATE, -1);
        begin=Timestamp.valueOf(format.format(cBegin.getTime()));
        end=Timestamp.valueOf(format.format(cEnd.getTime()));
        System.out.println(begin+"            "+end);
        //加入返回数据的1天前部分
        dateInfo.setTodayBefore6_begin(begin);
        dateInfo.setTodayBefore6_end(end);

        return dateInfo;

    }


    //返回这个月等6个月的日期信息
    public DateInfo getMonthDateInfo(){

        DateInfo dateInfo=new DateInfo();

        //一个月的开始时间和结束时间
        Timestamp begin;
        Timestamp end;

        Calendar c=Calendar.getInstance();
        Calendar cBegin=Calendar.getInstance();
        Calendar cEnd=Calendar.getInstance();

        //本月天数
        int MaxDay=c.getActualMaximum(Calendar.DAY_OF_MONTH);

        cBegin.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1, 0, 0, 0);
        cEnd.set( c.get(Calendar.YEAR), c.get(Calendar.MONTH), MaxDay, 23, 59, 59);

        SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //得到这个月的起始以及结束时间
        begin=Timestamp.valueOf(format.format(cBegin.getTime()));
        end=Timestamp.valueOf(format.format(cEnd.getTime()));
        System.out.println(begin+"            "+end);
        //加入返回数据的这个月部分
        dateInfo.setToday_begin(begin);
        dateInfo.setToday_end(end);


        //得到1个月前的起始以及结束时间
        cBegin.add(Calendar.MONTH, -1);
        cEnd.add(Calendar.MONTH, -1);
        begin=Timestamp.valueOf(format.format(cBegin.getTime()));
        end=Timestamp.valueOf(format.format(cEnd.getTime()));
        System.out.println(begin+"            "+end);
        //加入返回数据的1天前部分
        dateInfo.setTodayBefore1_begin(begin);
        dateInfo.setTodayBefore1_end(end);

        //得到2个月前的起始以及结束时间
        cBegin.add(Calendar.MONTH, -1);
        cEnd.add(Calendar.MONTH, -1);
        begin=Timestamp.valueOf(format.format(cBegin.getTime()));
        end=Timestamp.valueOf(format.format(cEnd.getTime()));
        System.out.println(begin+"            "+end);
        //加入返回数据的2天前部分
        dateInfo.setTodayBefore2_begin(begin);
        dateInfo.setTodayBefore2_end(end);


        //得到3个月前的起始以及结束时间
        cBegin.add(Calendar.MONTH, -1);
        cEnd.add(Calendar.MONTH, -1);
        begin=Timestamp.valueOf(format.format(cBegin.getTime()));
        end=Timestamp.valueOf(format.format(cEnd.getTime()));
        System.out.println(begin+"            "+end);
        //加入返回数据的3天前部分
        dateInfo.setTodayBefore3_begin(begin);
        dateInfo.setTodayBefore3_end(end);


        //得到4个月前的起始以及结束时间
        cBegin.add(Calendar.MONTH, -1);
        cEnd.add(Calendar.MONTH, -1);
        begin=Timestamp.valueOf(format.format(cBegin.getTime()));
        end=Timestamp.valueOf(format.format(cEnd.getTime()));
        System.out.println(begin+"            "+end);
        //加入返回数据的4天前部分
        dateInfo.setTodayBefore4_begin(begin);
        dateInfo.setTodayBefore4_end(end);


        //得到5个月前的起始以及结束时间
        cBegin.add(Calendar.MONTH, -1);
        cEnd.add(Calendar.MONTH, -1);
        begin=Timestamp.valueOf(format.format(cBegin.getTime()));
        end=Timestamp.valueOf(format.format(cEnd.getTime()));
        System.out.println(begin+"            "+end);
        //加入返回数据的5天前部分
        dateInfo.setTodayBefore5_begin(begin);
        dateInfo.setTodayBefore5_end(end);


        //得到6个月前的起始以及结束时间
        cBegin.add(Calendar.MONTH, -1);
        cEnd.add(Calendar.MONTH, -1);
        begin=Timestamp.valueOf(format.format(cBegin.getTime()));
        end=Timestamp.valueOf(format.format(cEnd.getTime()));
        System.out.println(begin+"            "+end);
        //加入返回数据的6天前部分
        dateInfo.setTodayBefore6_begin(begin);
        dateInfo.setTodayBefore6_end(end);

        return dateInfo;

    }


    //复制整个文件夹内容
    public  void  copyFolder(String  oldPath,  String  newPath)  {

        try  {
            (new File(newPath)).mkdirs();  //如果文件夹不存在  则建立新文件夹
            File  a=new  File(oldPath);
            String[]  file=a.list();
            File  temp=null;
            for  (int  i  =  0;  i  <  file.length;  i++)  {
                if(oldPath.endsWith(File.separator)){
                    temp=new  File(oldPath+file[i]);
                }
                else{
                    temp=new  File(oldPath+File.separator+file[i]);
                }

                if(temp.isFile()){
                    FileInputStream input  =  new  FileInputStream(temp);
                    FileOutputStream output  =  new  FileOutputStream(newPath  +  "/"  +
                            (temp.getName()).toString());
                    byte[]  b  =  new  byte[1024  *  5];
                    int  len;
                    while  (  (len  =  input.read(b))  !=  -1)  {
                        output.write(b,  0,  len);
                    }
                    output.flush();
                    output.close();
                    input.close();
                }
                if(temp.isDirectory()){//如果是子文件夹
                    copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
                }
            }
        }
        catch  (Exception  e)  {
            System.out.println("复制整个文件夹内容操作出错");
            e.printStackTrace();
        }
    }

    //根据头像，复制头像到服务器，并返回头像地址
    public String copyIcon(MultipartFile file){

        String finalFilePath="";

        //需要copy到的path
        String path2 = SportServerSymbol.tomcatImgPath;

        //图片本来的存储，暂时可以写死
        String path = SportServerSymbol.ImgStoragePath;;

        String fileName = file.getOriginalFilename();
        System.out.println(path);
        String prefix = "";
        String stuffix = "";

        if(fileName.indexOf(".") >= 0)
        {
            int indexdot = fileName.indexOf(".");
            stuffix = fileName.substring(indexdot);

            prefix = fileName.substring(0,fileName.indexOf("."));
            Date now = new Date();
            prefix = prefix+"_"+now.getTime();
            fileName = prefix + stuffix;
        }


        System.out.println(fileName);

        File targetFile = new File(path,fileName);
        if(!targetFile.exists())
        {
            targetFile.mkdirs();
        }

        //保存
        try{
            file.transferTo(targetFile);
            copyFolder(path,path2);                                 //copy到服务器的文件夹下面
        }catch (Exception e)
        {
            e.printStackTrace();
        }

        finalFilePath="/0/upload_img/"+fileName;
        return finalFilePath;

    }

}

